﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp1.ViewModels;

using System.Net.Http;
using System.Net.Http.Headers;
using System.Configuration;

using System.Net;
using System.Net.Http;
using System.Net.Http.Json;

using WpfApp1.Models;
using WpfApp1.Services;
using WpfApp1.Views;

using Newtonsoft.Json;

using System.ComponentModel;
using System.Collections.ObjectModel;

using Gardener.Common;

using System.IO;

//正则表达式的名称空间
using System.Text.RegularExpressions;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //内存中维护的目录树文件
        IList<Item> folderFileTree;
        MainViewModel viewModel = new MainViewModel();
        public MainWindow()
        {
            InitializeComponent();

            //测试用
            viewModel.StudentLists = new ObservableCollection<SourceDto>()
            {
                new SourceDto(){ SourceName="sss", SourcePath = "ccc"},
                new SourceDto(){ SourceName="eee", SourcePath = "vvv"}

            };

            this.DataContext = viewModel;
            this.lbStudent.DataContext = viewModel;

            //将内存目录树的上下文绑定到相应的模型上
            this.treeView1.ItemsSource = viewModel.FileItems;


            httpClient.BaseAddress = new Uri("https://localhost:44320");
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));

            //申荣杰加
            //Directory_Load();
            //自动更新筛选文件的信息表头
            fileInfo.AutoGeneratingColumn += fileInfoColumn_Load;

            //初始化配置文件
            ConfigService.InitConfig();
        }


        private void fileInfoColumn_Load(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            List<string> requiredProperties = new List<string>
            {
                "Name",  "LastWriteTime"
            };

            if (!requiredProperties.Contains(e.PropertyName))
            {
                e.Cancel = true;
            }
            else
            {
                e.Column.Header = e.Column.Header.ToString();
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            var path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string jsonfile = @"E:\gitrepoprojects\Furion\samples\Furion.Application\bin\Debug\net5.0\applicationsettings.json";       //JSON文件路径

            using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject o = (JObject)JToken.ReadFrom(reader);
                    var value = o["DatabaseSettings"].Value<string>("connectionString");
                    return;
                }
            }

        }

        /// <summary>
        /// 本地进行上传文件的测试工作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        
        private async void UploadFile(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("yuxi");
            MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent();

            StreamContent streamConent = new StreamContent(new FileStream(@"E:\gitrepoprojects\测试用文件夹\窗嵌板_70-90 系列四扇推拉铝窗.rfa", FileMode.Open, FileAccess.Read, FileShare.Read));
            streamConent.Headers.Add("Content-Type", "multipart/form-data");

            multipartFormDataContent.Add(streamConent, "file", @"E:\gitrepoprojects\测试用文件夹\窗嵌板_70-90 系列四扇推拉铝窗.rfa");
            string a = "eee";

            var respone = await httpClient.PostAsync($"/api/file/provider-upload", multipartFormDataContent);


        }

        /// <summary>
        /// 上传文件的运行函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void UploadFileFunc(string localFilePath)
        {
            MessageBox.Show("yuxi");
            MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent();

            StreamContent streamConent = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read, FileShare.Read));
            streamConent.Headers.Add("Content-Type", "multipart/form-data");

            multipartFormDataContent.Add(streamConent, "file", localFilePath);
            string a = "eee";

            var respone = await httpClient.PostAsync($"/api/file/provider-upload", multipartFormDataContent);


        }

    }
}
